﻿using ByteSite.Common;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;

namespace ByteSite.QLYCCatch
{
    class Program
    {
        static void Main(string[] args)
        {

            ThreadStart ts1 = new ThreadStart(qcatch1);
            Thread th1 = new Thread(ts1);
            th1.Start();

            ThreadStart ts2 = new ThreadStart(qcatch2);
            Thread th2 = new Thread(ts2);
            th2.Start();

            ThreadStart ts3 = new ThreadStart(qcatch3);
            Thread th3 = new Thread(ts3);
            th3.Start();

            ThreadStart ts4 = new ThreadStart(qcatch4);
            Thread th4 = new Thread(ts4);
            th4.Start();

            ThreadStart ts5 = new ThreadStart(qcatch5);
            Thread th5 = new Thread(ts5);
            th5.Start();

            ThreadStart ts6 = new ThreadStart(qcatch6);
            Thread th6 = new Thread(ts6);
            th6.Start();

            ThreadStart ts7 = new ThreadStart(qcatch7);
            Thread th7 = new Thread(ts7);
            th7.Start();

            ThreadStart ts8 = new ThreadStart(qcatch8);
            Thread th8 = new Thread(ts8);
            th8.Start();

            ThreadStart ts9 = new ThreadStart(qcatch9);
            Thread th9 = new Thread(ts9);
            th9.Start();

            ThreadStart ts10 = new ThreadStart(qcatch10);
            Thread th10 = new Thread(ts10);
            th10.Start();

            ThreadStart ts11= new ThreadStart(qcatch11);
            Thread th11 = new Thread(ts11);
            th11.Start();
        }
        static void qcatch1()
        {
            QLYCCatch(1001, 100000);
        }
        static void qcatch2()
        {
            QLYCCatch(100001, 200000);
        }
        static void qcatch3()
        {
            QLYCCatch(200001, 300000);
        }
        static void qcatch4()
        {
            QLYCCatch(300001, 400000);
        }
        static void qcatch5()
        {
            QLYCCatch(400001, 500000);
        }
        static void qcatch6()
        {
            QLYCCatch(500001, 600000);
        }
        static void qcatch7()
        {
            QLYCCatch(600001, 700000);
        }
        static void qcatch8()
        {
            QLYCCatch(700001, 800000);
        }
        static void qcatch9()
        {
            QLYCCatch(800001, 900000);
        }
        static void qcatch10()
        {
            QLYCCatch(900001, 1000000);
        }
        static void qcatch11()
        {
            QLYCCatch(10000001, 1100000);
        }

        static void QLYCCatch(int min,int max)
        {
            for (int id = min; id <= max; id++)
            {
                if (!System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "/qlyc/" + id + ".txt"))
                {

                    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //加上这一句
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://ggzyjyzx.shandong.gov.cn/wssc/poor_support/goods/" + id.ToString());
                    request.Method = "GET";
                    request.Headers.Add("siteCode", "sdszfcg");
                    //request.ContentType = "application/x-www-form-urlencoded";
                    request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
                    request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36";

                    request.Timeout = 15000;
                    request.AllowAutoRedirect = false;

                    WebResponse response = null;
                    string responseStr = null;

                    try
                    {
                        response = request.GetResponse();

                        if (response != null)
                        {
                            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                            responseStr = reader.ReadToEnd();
                            reader.Close();
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        request = null;
                        response = null;
                    }


                    // 判断文件是否存在，不存在则创建，否则读取值显示到txt文档
                    if (System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "/qlyc/" + id + ".txt"))
                    {
                        //File.Delete(AppDomain.CurrentDomain.BaseDirectory + "/qlyc/" + id + ".txt");
                    }

                    FileStream fs1 = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "/qlyc/" + id + ".txt", FileMode.Create, FileAccess.Write);//创建写入文件 
                    StreamWriter sw = new StreamWriter(fs1);
                    sw.WriteLine(responseStr);//开始写入值
                    sw.Close();
                    fs1.Close();
                }
            }
        }
    }
}
